package com.fzrskj.cloud.free.user.service.member;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.user.service.member.vo.DetailImportItemVo;
import com.fzrskj.cloud.free.user.utils.member.DetailImportUtils;
import com.fzrskj.cloud.free.user.utils.proin.ExcelReadUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * MemberImportService.
 *
 * @author zj.
 *         Created on 2019/5/8 0008.
 */
public class DetailImportService extends CObject {
    /**
     * 会员导入：文件上传.
     */
    public String memberImportDetailUpload (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        List<DetailImportItemVo> detailList = new ArrayList<>();
        try {
            String[] keys = new String[10];
            keys[0] = "userName";
            keys[1] = "amount";
            keys[2] = "balance";
            keys[3] = "bankCodeName";
            keys[4] = "billNo";
            keys[5] = "itemIdName";
            keys[6] = "regDate";
            keys[7] = "remarkRecord";
            keys[8] = "snId";
            keys[9] = "voucherId";

            //从excel中读取
            detailList = ExcelReadUtils.readXls(req, DetailImportItemVo.class, keys, 1, 1);

            //预先过滤
            DetailImportUtils importUtils = new DetailImportUtils();
            detailList = importUtils.refer(detailList);

        } catch (Exception e) {
            return XmlCodeParser.formatResultTips(false, "ERROR",
                    "读取csv失败，请检查导入内容。异常信息：" + e.getMessage());
        }
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(detailList));
    }

    /**
     * 会员导入：保存.
     */
    public String memberImportDetail (HttpServletRequest req, HttpServletResponse rsp, String p, String sid) {
        List<DetailImportItemVo> memberList = CJsonTool.getListValue(p, "details", new ArrayList<>(), DetailImportItemVo.class);
        if (memberList.isEmpty()) {
            return XmlCodeParser.formatResultTips(false, "ERROR", "无导入数据");
        }

        DetailImportUtils importUtils = new DetailImportUtils();
        memberList = importUtils.save(memberList);
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(memberList));
    }
}
